package middleware

import (
	"gateway-server/internal/types"
	"gateway-server/pkg/beego"
	"strings"
)

func UserAuth() {
	beego.InsertGlobalFilterChain(func(ctx *beego.BeeCtx, next beego.BeeNextFunc) {
		authHeader := ctx.Request.Header.Get(types.AuthorizationHeader)
		if authHeader == "" {
			ctx.Abort(401, "Authorization header is missing")
			return
		}
		var userJWT string
		if len(authHeader) > 7 && authHeader[:7] == types.AuthorizationPrefix {
			userJWT = strings.TrimSpace(authHeader[7:])
		} else {
			ctx.Abort(401, "Invalid Authorization header")
			return
		}
		ctx.Input.SetData("user_jwt", userJWT)
		next(ctx)
	})
}
